using System;
using Microsoft.SPOT;

namespace A6281 {
	public class DelaySet : ICyclicEnumerable {

		internal class DelaySetEnumerator : ICyclicEnumerator {

			public DelaySetEnumerator( DelaySet mother ) {
				this.mother = mother;
				this.Reset();
			}

			public object Current {
				get {
					lock( this.indexSync ) {
						return this.mother.GetDelayByIndex( this.index );
					}
				}
			}

			public bool MoveNext() {
				lock( this.indexSync ) {
					this.index = ( uint )( this.index + 1 ) % this.mother.GetDelayLength();
				}
				return true;
			}

			public void Reset() {
				lock( this.indexSync ) {
					this.index = 0;
				}
			}

			protected DelaySet mother;
			protected object indexSync = new object();
			protected uint index;
		}

		public DelaySet( params Delay[] delayArray ) {
			this.delayArray = delayArray;
		}

		public DelaySet( params int[] durationArray ) {
			this.delayArray = new Delay[ durationArray.Length ];
			for ( int index = 0; index < durationArray.Length; index++ ) {
				this.delayArray[ index ] = new Delay( durationArray[ index ] );
			}
		}

		public System.Collections.IEnumerator GetEnumerator() {
			return new DelaySetEnumerator( this );
		}

		public Delay GetDelayByIndex( uint index ) {
			return this.delayArray[ index ];
		}

		public uint GetDelayLength() {
			return ( uint )this.delayArray.Length;
		}

		protected object delayArraySync = new object();
		protected Delay[] delayArray;

	}
}
